package com.yingside.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TestMousePaintFrame extends JFrame implements ActionListener {
    private int x,y;
    private List<Pointer> pointerList;
    private final static int RADIUS = 20;
    //菜单栏
    private JMenuBar menuBar;

    //右键菜单
    private JPopupMenu popupMenu;

    public TestMousePaintFrame() {
        this.setTitle("鼠标事件窗口");
        this.setBounds(300, 300, 400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        popupMenu = new JPopupMenu();
        JMenuItem popSave = new JMenuItem("保存");
        popupMenu.add(popSave);
        JMenuItem popLoad = new JMenuItem("读取");
        popupMenu.add(popLoad);

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(e.isPopupTrigger()){
                    popupMenu.show(e.getComponent(),e.getX(),e.getY());
                }
            }
        });

        popSave.addActionListener(new OutputActionListener());
        popLoad.addActionListener(this);

        menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("文件(F)");
        fileMenu.setMnemonic('f');

        JMenuItem saveItem = new JMenuItem("保存(S)");
        saveItem.setMnemonic('s');
        JMenuItem loadItem = new JMenuItem("读取(L)");
        loadItem.setMnemonic('l');
        JMenuItem exitItem = new JMenuItem("退出(X)");
        exitItem.setMnemonic('x');

        fileMenu.add(saveItem);
        fileMenu.add(loadItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);


        menuBar.add(fileMenu);

        saveItem.addActionListener(new OutputActionListener());

        /*saveItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ObjectOutputStream oos = null;

                try {
                    oos = new ObjectOutputStream(
                            new BufferedOutputStream(
                                    new FileOutputStream(
                                            new File("images/paint.sb"))));

                    oos.writeObject(pointerList);
                } catch (IOException e1) {
                    e1.printStackTrace();
                } finally {
                    try {
                        oos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });*/


        loadItem.addActionListener(this);
        /*
        loadItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ObjectInputStream ois = null;
                try {
                    ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("images/paint.sb"))));
                    pointerList = (List<Pointer>)ois.readObject();
                    repaint();
                } catch (IOException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();
                } finally {
                    try {
                        ois.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
        */


        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });


        pointerList = new ArrayList<>();

        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                x = e.getX();
                y = e.getY();

                Pointer p = new Pointer(x,y);

                pointerList.add(p);
                //重绘窗口
                repaint();
            }
        });
    }

    //paint不需要我们调用，java虚拟机会自动的调用paint方法
    public void paint(Graphics g) {
        super.paint(g);

        for (int i=0; i<pointerList.size(); i++){
            Pointer p = pointerList.get(i);
            g.fillOval(p.getX(),p.getY(),RADIUS,RADIUS);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("images/paint.sb"))));
            pointerList = (List<Pointer>)ois.readObject();
            repaint();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

    private class OutputActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            ObjectOutputStream oos = null;

            try {
                oos = new ObjectOutputStream(
                        new BufferedOutputStream(
                                new FileOutputStream(
                                        new File("images/paint.sb"))));

                oos.writeObject(pointerList);
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    oos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        TestMousePaintFrame testPaintFrame = new TestMousePaintFrame();
        testPaintFrame.setVisible(true);

    }
}
